"""Example Task Structure A standard task must follow these:

 A model class inherit from MVC.plstdocument.IDocument with
 filename+Task as class name. This class must implement run() method

 A class named filename+Adapter with handle_request() implemented

 A class named filename+View inherit form Frame  with show()
 and Update() implemented

 for example:
 if a new task named Example.py . It must have
 ExampleTask, ExampleAdapter and ExampleView classes

"""

import sys
import os
sys.path.append(os.path.abspath('..'))
from service.PlusterLog import PlusterLog
from service.pconfig import PConfig
from MVC.plstdocument import IDocument
from Tkinter import *
from tkMessageBox import *

class ExampleTask (PlusterLog,IDocument):
    """ Example Task Model """
    def __init__ (self):
        """ Setup Example"""
        PlusterLog.__init__(self)
        IDocument.__init__(self)
        self.name = ''
    def run (self):
        """ Start Task"""
        cfg = PConfig()
        self.name = cfg.attribute.cluster_name
        self.Flush()#update view
    def config (self,*arg):
        """ Config task before it start"""
        pass



class ExampleAdapter:
    def __init__ (self,doc):
        """ Adapter knows how to call model and how to communicate
    with other service to provide enough info to invoke method''"""
        self.doc = doc
    def handle_update_name (self):
        """ Read System name from PConfig"""
        self.doc.run()


class ExampleView(Frame):
    """ You can create a view by using VBtk class or create it
    manually. Here we just show how to do it by hand"""
    def __init__ (self,ctl):
        """ Setup code for View"""
        self.ctl = ctl

    def show (self, master):
        """ Generate UI"""
        self.window = Toplevel(master)
        Frame.__init__(self,self.window)
        self.master.title('Example Task')
        self.grid()
        Label(self,text = "System name is: ").grid(row=0)
        self.l_name = Label(self, text="I don't know my name")
        self.l_name.grid(row=0, column=1)
        self.button=Button(self, text="Read System Name", \
                               command=self.button_handler)
        self.button.grid(row=1,column=1)

    def button_handler (self):
        """ Handler"""
        self.ctl.handle_update_name()


    def Update (self, doc):
        """ Update UI when doc.Flush()"""
        self.l_name['text']=doc.name










